<template>
	<xn-form-container
		:title="formData.reportId ? '编辑隐患上报信息' : '增加隐患上报信息'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="上报人姓名：" name="userName">
						<a-input v-model:value="formData.userName" placeholder="请输入上报人姓名" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="上报人电话：" name="userPhone">
						<a-input v-model:value="formData.userPhone" placeholder="请输入上报人电话" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="隐患类型" name="hiddenType">
						<a-select v-model:value="formData.hiddenType" placeholder="请选择隐患类型"
								  :options="hiddenTypeOptions" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="隐患上报位置：" name="address">
						<a-input v-model:value="formData.address" placeholder="请输入隐患上报位置" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="隐患描述：" name="hiddenRemark">
						<a-textarea v-model:value="formData.hiddenRemark" placeholder="请输入隐患描述" :rows="4"
									allow-clear :maxlength="200"/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="图片附件" name="fliePath" extra="最大可上传5张图片">
						<SingPicUpload ref="fliePathUploadRef" :limitImage="5"></SingPicUpload>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="视频文件：" name="videoPath" extra="可上传1个200M内的短视频">
						<UploadFile ref="videoPathUploadRef" :limitImage="1"></UploadFile>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="声音文件：" name="soundPath" extra="可上传1个语音文件">
						<UploadFile ref="soundPathUploadRef" :limitImage="1"></UploadFile>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="hiddenReportForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import hiddenReportApi from '@/api/biz/hidden/hiddenReportApi'
	import tool from "@/utils/tool"
	import SingPicUpload from '@/components/SingPicUpload/index.vue'
	import UploadFile from '@/components/FileUpload/index.vue'

	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const fliePathUploadRef = ref()
	const videoPathUploadRef = ref()
	const soundPathUploadRef = ref()

	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const hiddenTypeOptions = tool.dictList('hiddenType')

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		nextTick(() => {
			if (record && record.fliePath) {
				fliePathUploadRef.value.initFileList(record.fliePath)
			}
			if (record && record.videoPath) {
				videoPathUploadRef.value.initFileList(record.videoPath)
			}
			if (record && record.soundPath) {
				soundPathUploadRef.value.initFileList(record.soundPath)
			}
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	// 默认要校验的
	const formRules = {
		userName: [required('请填写上报人姓名')],
		userPhone: [required('请填写上报人电话')],
		hiddenType: [required('请选择隐患类型')],
		address: [required('请填写隐患上报位置')],
		hiddenRemark: [required('请填写隐患描述')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formData.value.fliePath = fliePathUploadRef.value.buildUrl()
		formData.value.videoPath = videoPathUploadRef.value.buildUrl()
		formData.value.soundPath = soundPathUploadRef.value.buildUrl()
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			hiddenReportApi
				.hiddenReportSubmitForm(formDataParam, formDataParam.reportId)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
